{ "cells": [ { "cell_type": "markdown", "id": "d240914c", "metadata": {}, "source": [ "## String Functions" ] }, { "cell_type": "markdown", "id": "54e12334", "metadata": {}, "source": [ "| Description | Function |\n", "|---|---|\n", "| Add leading 0's to str to full the length | str.zfill(5) |\n", "| Convert to upper case | str.upper() |\n", "| Convert to lower case | str.lower() |\n", "| Check if upper case | str.isupper() |\n", "| Check if lower case | str.islower() |\n", "| Check if string is only numbers | str.isdigit() |\n", "| Check if string is only characters | str.isalpha() |\n", "| Check if string is chars+numbers | str.isalnum() |\n", "| Check if string is only spaces and not blank | str.isspace() |\n", "| Remove white spaces from string | str.strip() (l,r also) |\n", "| Remove 0's from left of string | str.lstrip(0) |\n", "| Remove 0's from right of string | str.rstrip(0) |\n", "| Remove 0's from string | str.strip(0) |\n", "| Remove anything from string | str.strip('abc') |\n", "| Replace anything from string | str.replace('old','new') |\n", "| Remove anything from string | str.strip('abc') |\n", "| Check if string starts with abc | str.startswith('abc') |\n", "| Check if string ends with abc | str.endswith('abc') |\n", "| Check if string contains 'abc' | if 'abc' in str |\n", "| Check if string doesn't contains 'abc' | if 'abc' not in str |\n", "| Concatenation or making a string dynamic | 'Hi {name}'.format(name='Sahil') |" ] }, { "cell_type": "markdown", "id": "ad96abfa", "metadata": {}, "source": [ "### Note : \n", "\n", " - Space is considered as number in python so will be detected in .isnum()\n", " - Nan is float in pandas\n", " - Left,Right and Mid() functions are not available in python but you can use string slicing to get the same results'\n", " - String functions can be chained as well\n", " - str.replace().replace()" ] }, { "cell_type": "markdown", "id": "424b7aa5", "metadata": {}, "source": [ "### Example of method chaining" ] }, { "cell_type": "code", "execution_count": 3, "id": "751afa58", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'AB123ABVba'" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s='ab123abvba'\n", "s.replace('ab','AB').replace('v','V')" ] }, { "cell_type": "markdown", "id": "e52bd77b", "metadata": {}, "source": [ "### Applying operations on multiple columns" ] }, { "cell_type": "markdown", "id": "c5789297", "metadata": {}, "source": [ "- Lets say you want to convert datatype of multiple columns\n", "\n", "\n", "- Format to convert datatype of one column\n", " - ```df['col'].astype(int)```\n", " \n", " \n", "- Format to convert datatype of multiple columns\n", " - Store the list of col names in list\n", " - ```list=['A','B','C']```\n", " \n", " - Iterate through the list and do the operation on single single element of iterable\n", " - ```for i in list:\n", " df[i].astype(int)```" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.8" } }, "nbformat": 4, "nbformat_minor": 5 }